package service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import bean.User;
import bean.UserRank;
import dao.UserDAOImpl;

public class UserService {

    /**
     * 添加用户结果码
     * 0 成功
     * 1 该微信openid已注册
     * 2 该手机号已注册
     */
    public static final int ADD_USER_SUCCESS = 0;
    public static final int ADD_USER_EXIST_OPEN_ID = 1;
    public static final int ADD_USER_EXIST_PHONE = 2;
    
    private UserDAOImpl userDAOImpl;
    
    public void setUserDAOImpl(UserDAOImpl userDAOImpl) {
        this.userDAOImpl = userDAOImpl;
    }

    /**
     * 是否存在openId的用户
     * @param openId
     * @return
     */
    public boolean exists(String openId) {
        if (openId == null) {
            return false;
        }
        
        List<User> list = userDAOImpl.findByOpenId(openId);
        return list != null && list.size() > 0;
    }

    /**
     * 是否存在手机号为phone的用户
     * @param phone
     * @return
     */
    public boolean existsPhone(String phone) {
        if (phone == null) {
            return false;
        }
        
        List<User> list = userDAOImpl.findByPhone(phone);
        return list != null && list.size() > 0;
    }
    
    /**
     * 添加用户，返回结果码
     * 
     * @param user {User} 将要添加的用户对象
     * @return     {int}  结果码
     * 
     * @see UserService
     * ADD_USER_SUCCESS       0 成功
     * ADD_USER_EXIST_OPEN_ID 1 该微信openid已注册
     * ADD_USER_EXIST_PHONE   2 该手机号已注册
     */
    public int addUser(User user) {
        if (exists(user.getOpenId())) {
            return ADD_USER_EXIST_OPEN_ID;
        } else if (existsPhone(user.getPhone())) {
            return ADD_USER_EXIST_PHONE;
        }
        userDAOImpl.save(user);
        return ADD_USER_SUCCESS;
    }

    /**
     * 根据openId获取用户
     * @param openId
     * @return
     */
    public User getByOpenId(String openId) {
        if (openId == null) {
            return null;
        }
        List<User> list = userDAOImpl.findByOpenId(openId);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }
    

    /**
     * 根据id获取用户
     * @param id
     * @return
     */
    public User getByUserId(int userId) {

        User user = userDAOImpl.findById(userId);
        return user;
    }

    /**
     * 获取排名
     * 支持量大于support的数量
     * @param support 支持量
     * @return
     */
    public int getRank(int support) {
        return userDAOImpl.getRank(support);
    }

    /**
     * 用户投票排行榜
     * @param queryKey 模糊查询关键字
     * @param limit    每页数量
     * @param offset   偏移量（页数*每页数量）
     * @return         排行榜（用户信息+排名）
     */
    public List<UserRank> query(String queryKey, int limit, int offset) {
        queryKey = queryKey == null ? "" : queryKey.trim();
        
        // 分页查询用户
        List<User> users = userDAOImpl.query(queryKey, limit, offset);
        
        // 添加排名
        List<UserRank> urs = new ArrayList<UserRank>();
        int lastSupport = -1, lastRank = -1;
        for (User u : users) {
            // 新的排行数据
            if (u.getSupport() != lastSupport) {
                lastSupport = u.getSupport();
                lastRank = this.getRank(lastSupport);
            }
            
            urs.add(new UserRank(u, lastRank));
        }
        
        return urs;
    }

    /**
     * 查询用户总数
     * @param queryKey 模糊查询关键字
     * @return         在某关键字下查询到的用户总数
     */
    public int queryCount(String queryKey) {
        queryKey = queryKey == null ? "" : queryKey.trim();
        return userDAOImpl.queryCount(queryKey);
    }
    
    public void updateUserInfoByOpenId(String openId, String userName, String headImgUrl){
        userDAOImpl.updateUserInfoByOpenId(openId, userName, headImgUrl);
    }
    
}
